﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BlueCMS.Core.Entities;
using BlueCMS.Core.Data.Query;

namespace BlueCMS.Core.Services
{
    public class ActionService : ServiceBase
    {
        public static IList<ActionMenuItem> GetActionsForPageletInstance(PageletInstance instance)
        {
            SelectQuery query = new SelectQuery();
            query.Select
                .Column("n.node_id")
                .Column("a.title");
            query.From
                .Table("cms_action_tbl").As("a")
                .Join("cms_action_node_lnk").As("anl").On(c => c.Column("anl.action_id").Equals().Column("a.action_id"))
                .Join("cms_node_tbl").As("n").On(c => c.Column("n.node_id").Equals().Column("anl.node_id"))
                .Join("cms_nodetype_tbl").As("nt").On(c => c.Column("nt.nodetype_id").Equals().Column("n.nodetype_id"))
                .Join("cms_nodeinheritsfrom_vw").As("nif").On(c => c.Column("n.node_id").Equals().Column("nif.node_id"))
                .Join("cms_node_permission_role_lnk").As("npr").On(c => c.Column("npr.node_id").Equals().Column("nif.inheritsfrom_id"))
                .Join("cms_permission_tbl").As("p").On(c => c.Column("npr.permission_id").Equals().Column("p.permission_id"))
                .Join("cms_user_role_lnk").As("ur").On(c => c.Column("npr.role_id").Equals().Column("ur.role_id"));
            query.Where
                .Column("n.parent_id").Equals().Parameter(instance.node_id)
                .And()
                .Column("ur.user_id").Equals().Parameter(CmsApplication.CmsUser.user_id)
                .And()
                .Column("p.object_cd").Equals().Constant("NODE")
                .And()
                .Column("p.permission_cd").Equals().Constant("VIEW")
                .And()
                .Column("n.isactive").Equals().Constant(true)
                .And()
                .Column("n.isdeleted").Equals().Constant(false);
            query.OrderBy
                .Column("a.displayorder")
                .Column("a.title");

            return query.ExecuteCollection<ActionMenuItem>();
        }

        public static ActionNode GetActionNodeForNode(Node node)
        {
            ActionNode actionNode = new ActionNode();
            actionNode.node = node;
            actionNode.parent = NodeService.GetNodeForId(node.parent_id.Value);

            SelectQuery query = new SelectQuery();
            query.Select.Column("a.*");
            query.From
                .Table("cms_action_tbl").As("a")
                .Join("cms_action_node_lnk").As("anl").On(c => c.Column("a.action_id").Equals().Column("anl.action_id"));
            query.Where.Column("anl.node_id").Equals().Parameter(node.node_id);
            actionNode.action = query.ExecuteObject<BlueCMS.Core.Entities.Action>();

            query = new SelectQuery();
            query.Select.All();
            query.From.Table("cms_plugin_tbl");
            query.Where.Column("plugin_id").Equals().Parameter(actionNode.action.plugin_id);
            actionNode.plugin = query.ExecuteObject<Plugin>();

            return actionNode;
        }
    }
}
